/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


/**
 *
 * @author Carlos
 */
public class Menu extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throw SocialFP red;s IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
         SocialFP red;
        /*try{
               GestorPersistencia.crearConexion();
        }catch(ErrorConexionBD e){
                  notificarError (request, response, "Error", "Error al  conectar con la BBDD");     
                }
        red=new SocialFP();
             
        try {
                 red.recuperaUjaenSocial();
        }catch (Exception e){
                 notificarError (request, response, "Error", "No se puede recuperar el estado de la red");
        }*/
         red =  (SocialFP) request.getSession ().getAttribute ("red");
         if (red == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
        
        //Elijo la opcion pulsada
        if (request.getParameter("Buscar")!=null)
            BuscarUsuarios(request,response,red);
        else
            if (request.getParameter("VerPrivado")!=null)
                VerPrivados(request,response,red);
            else
                if (request.getParameter("VerAmigos")!=null)
                VerAmigos ( request, response, red);
                else
                    if(request.getParameter("VerNotas")!=null)
                        VerNotas(request, response, red);
              
             else
                notificarError(request,response,"Error", "Acceso ilegal");
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    protected void BuscarUsuarios (HttpServletRequest request, HttpServletResponse response,SocialFP red)
        throws ServletException, IOException
    {
             //Pido el término
        
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                 // Imprimir cabecera
        out.println ("<body alink=\"#000099\" bgcolor=\"#6d9fff\" link=\"#000099\" text=\"#000000\" vlink=\"#990099\">");        
        out.println ("<table cellspacing=\"5\" bgcolor=\"#2c90c9\" width = \"600\">");
        out.println ("<tr><td><img src=\"imagenes/cabecera.jpg\" align=\"bottom\" height=\"100\" width=\"80\"></td>");
        out.println ("<td align=\"left\" td valign=\"middle\"><font color=\"#f9f9f9\"><big><big><big><b>Social FP</b></font></big></big></big></td></tr>");
        out.println ("</table>");
        
         
                out.println ("<form action=\"" + response.encodeURL ("BuscaUsers") + "\" method=\"post\">");
                out.println ("<label for=\"termino\" accesskey=\"T\">Término para buscar: </label>");
                  out.println("<input type=\"text\" name=\"termino\" size=15><br><br>");
                  out.println ("<input type=\"submit\" name=\"Aceptar\" value=\"Aceptar\" accessKey=\"T\">");
                  out.println ("</form></td></tr>");
                  out.println ("</table>");
                  out.println ("</body>");
                  out.println ("</html>");
                  out.close();
                    
    }
    
     protected void VerPrivados (HttpServletRequest request, HttpServletResponse response,SocialFP red)
        throws ServletException, IOException
    {
    //Imprimo los amigos a los que puedo enviar privado
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         // Imprimir cabecera
        out.println ("<body alink=\"#000099\" bgcolor=\"#6d9fff\" link=\"#000099\" text=\"#000000\" vlink=\"#990099\">");
        out.println ("<table cellspacing=\"5\" bgcolor=\"#2c90c9\" width = \"600\">");
        out.println ("<tr><td><img src=\"imagenes/cabecera.jpg\" align=\"bottom\" height=\"100\" width=\"80\"></td>");
        out.println ("<td align=\"left\" td valign=\"middle\"><font color=\"#f9f9f9\"><big><big><big><b>Social FP</b></font></big></big></big></td></tr>");
        out.println ("</table>");
        
        out.println ("<table cellspacing=\"10\" bgcolor=\"#f0f0f0\" width = \"600\"><tr><td>");
        out.println ("<table width = \"100%\">");
        out.println ("<caption>Mensajes Privados</caption>");
        out.println ("<tr><th bgcolor=\"#e9e9e9\">Usuario</th><th bgcolor=\"#e9e9e9\">Mensaje</th>");
          
                String email=  (String) request.getSession ().getAttribute ("email");
                String clave=  (String) request.getSession ().getAttribute ("clave");
                Usuario u = red.loginUsuario(email, clave);
               ArrayList lista;
               Mensaje temp;
               lista= new ArrayList();
               lista=(ArrayList)u.leeMensajesPrivados();
               Iterator itera = lista.listIterator();
      		  while(itera.hasNext()){
                    temp=(Mensaje) itera.next();
                    out.println ("<tr>");
                    out.println ("<td bgcolor=\"#ffffff\" align=\"right\"><small>" + temp.leeEmisor()+ "</small></td>");
                    out.println ("<td bgcolor=\"#ffffff\" align=\"right\"><small>" + temp.leeContenido()+ "</small></td>");
                    out.println ("</tr>");
                    
                  }
               out.println ("</table>");
               out.println ("<form action=\"" + response.encodeURL ("mostrarUsuario") + "\" method=\"post\">");
               out.println ("<input type=\"submit\" name=\"Volver\" value=\"Volver\" accessKey=\"V\"><br>");                             
               out.println ("</form>");
               out.close();
    }
     
     protected void VerAmigos (HttpServletRequest request, HttpServletResponse response,SocialFP red)
        throws ServletException, IOException
    {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         // Imprimir cabecera
        out.println ("<body alink=\"#000099\" bgcolor=\"#6d9fff\" link=\"#000099\" text=\"#000000\" vlink=\"#990099\">");
        out.println ("<table cellspacing=\"5\" bgcolor=\"#2c90c9\" width = \"600\">");
        out.println ("<tr><td><img src=\"imagenes/cabecera.jpg\" align=\"bottom\" height=\"100\" width=\"80\"></td>");
        out.println ("<td align=\"left\" td valign=\"middle\"><font color=\"#f9f9f9\"><big><big><big><b>Social FP</b></font></big></big></big></td></tr>");
        out.println ("</table>");
        
        
         out.println ("<table cellspacing=\"10\" bgcolor=\"#f0f0f0\" width = \"600\"><tr><td>");
        out.println ("<table width = \"100%\">");
        out.println ("<caption>Amigos</caption>");
        out.println ("<tr><th bgcolor=\"#e9e9e9\">Usuario</th><th bgcolor=\"#e9e9e9\">Email</th>");
          
                String email=  (String) request.getSession ().getAttribute ("email");
                String clave=  (String) request.getSession ().getAttribute ("clave");
                Usuario u = red.loginUsuario(email, clave);
               ArrayList lista;
               Usuario temp;
               lista= new ArrayList();
               lista=(ArrayList)u.verAmigos();
               Iterator itera = lista.listIterator();
      		  while(itera.hasNext()){
                    temp=(Usuario) itera.next();
                    out.println ("<tr>");
                    out.println ("<td bgcolor=\"#ffffff\" align=\"right\"><small>" + temp.leeNombre()+ "</small></td>");
                    out.println ("<td bgcolor=\"#ffffff\" align=\"right\"><small>" + temp.leeEmail() + "</small></td>");
                    out.println ("</tr>");
                    }
               out.println ("</table>");
               out.println ("<form action=\"" + response.encodeURL ("mostrarUsuario") + "\" method=\"post\">");
               out.println ("<input type=\"submit\" name=\"Volver\" value=\"Volver\" accessKey=\"V\"><br>");                             
               out.println ("</form>");
               out.close();
    }
     
     protected void VerNotas (HttpServletRequest request, HttpServletResponse response,SocialFP red)
        throws ServletException, IOException
    {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         // Imprimir cabecera
        out.println ("<body alink=\"#000099\" bgcolor=\"#6d9fff\" link=\"#000099\" text=\"#000000\" vlink=\"#990099\">");
        out.println ("<table cellspacing=\"5\" bgcolor=\"#2c90c9\" width = \"600\">");
        out.println ("<tr><td><img src=\"imagenes/cabecera.jpg\" align=\"bottom\" height=\"100\" width=\"80\"></td>");
        out.println ("<td align=\"left\" td valign=\"middle\"><font color=\"#f9f9f9\"><big><big><big><b>SocialfP</b></font></big></big></big></td></tr>");
        out.println ("</table>");
        
        
                String email=  (String) request.getSession ().getAttribute ("email");
                String clave=  (String) request.getSession ().getAttribute ("clave");
                Usuario u = red.loginUsuario(email, clave);
        // Imprimimos notas       
        out.println ("<label for=\"Nota redes T1\" accesskey=\"S\">Nota redes T1: </label>");
        out.println ("<input type=\"text\" disabled name=\"redesT1\" size=20 value=\"" + u.leeNotas().leeNotaredesT1() + "\"><br>");
        out.println ("<label for=\"Nota redes T2\" accesskey=\"S\">Nota redes T2: </label>");
        out.println ("<input type=\"text\" disabled name=\"redesT1\" size=20 value=\"" + u.leeNotas().leeNotaredesT2() + "\"><br>");
        out.println ("<label for=\"Nota redes T3\" accesskey=\"S\">Nota redes T3: </label>");
        out.println ("<input type=\"text\" disabled name=\"redesT1\" size=20 value=\"" + u.leeNotas().leeNotaredesT3() + "\"><br>");
        out.println ("<label for=\"Nota hardware T1\" accesskey=\"S\">Nota hardware T1: </label>");
        out.println ("<input type=\"text\" disabled name=\"hardT1\" size=20 value=\"" + u.leeNotas().leeNotahardT1() + "\"><br>");
        out.println ("<label for=\"Nota hardware T2\" accesskey=\"S\">Nota hardware T2: </label>");
        out.println ("<input type=\"text\" disabled name=\"hardT2\" size=20 value=\"" + u.leeNotas().leeNotahardT2() + "\"><br>");
        out.println ("<label for=\"Nota hardwware T3\" accesskey=\"S\">Nota hardware T3: </label>");
        out.println ("<input type=\"text\" disabled name=\"hardT1\" size=20 value=\"" + u.leeNotas().leeNotahardT3() + "\"><br>");
        out.println ("<form action=\"" + response.encodeURL ("mostrarUsuario") + "\" method=\"post\">");
        out.println ("<input type=\"submit\" name=\"Volver\" value=\"Volver\" accessKey=\"V\"><br>");                             
        out.println ("</form>");
        out.println ("</body>");
        out.println ("</html>");
        out.close();
    }
    
    protected void notificarError (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=mostrarUsuario");
        dispatcher.forward (request, response);    
    }    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
